package oneroom.admin.member;
import java.util.ArrayList;
import java.util.List;
import java.sql.SQLException;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import com.ibatis.sqlmap.client.SqlMapClient;

import oneroom.comm.BaseUtil;
import oneroom.comm.IbatisLoader;
import oneroom.comm.ibatisDefault;

import org.apache.log4j.Logger;

public class MemberDao extends ibatisDefault{
	protected final Logger logger = Logger.getLogger(getClass());
	
	private IbatisLoader 	iBatis;
	private SqlMapClient 	sqlMapper;
	
	public MemberDao(){
		this.sqlMapper = iBatis.setConfig();
	}
	
	/**
	 * 회원 리스트 메소드
	 * @param paramMap (searchGubn,searchValue)
	 * @return ArrayList
	 */
	public ArrayList getList(HashMap paramMap){
		ArrayList returnList = new ArrayList();
		try{
			/**
			 * 페이징 처리 시작
			 */
			int page = Integer.parseInt((String)paramMap.get("giCurPage")); // 현제 페이지 번호
			if (page == 0) page = 1;
			int pageSize = Integer.parseInt((String)paramMap.get("pageSize")); //한페이지당 보여질 게시물의 갯 수
			if (pageSize == 0) pageSize = 10;
			int startPos = ((page-1)*pageSize);			
			
			paramMap.put("START",startPos);
			paramMap.put("STOP",pageSize);
			paramMap.put("memberGubn",(String)paramMap.get("memberGubn"));
			paramMap.put("searchGubn",(String)paramMap.get("searchGubn"));
			paramMap.put("searchValue",(String)paramMap.get("searchValue"));			
			
			cnt = ((Integer)sqlMapper.queryForObject("member.member_cnt",paramMap)).intValue();	
			returnList = (ArrayList)sqlMapper.queryForList("member.member_List",paramMap);
			
		}catch(Exception sqlEx){
			logger.error(sqlEx);
		}
		return returnList;
	}
	
	/**
	 * 회원 상세 페이지
	 * @param paramMap (idx)
	 * @return ArrayList
	 */
	public ArrayList getView(HashMap paramMap){
		ArrayList returnList = new ArrayList();
		try{
			returnList = (ArrayList)sqlMapper.queryForList("member.member_view",paramMap);
		}catch(Exception sqlEx){
			logger.error(sqlEx);
		}
		return returnList;
	}	

	/**
	 * 등록 수정시 들어 오는 것을 분기 해주는 곳
	 * @param request
	 * @return
	 */
	public HashMap bunki(HttpServletRequest request){
		HashMap returnValue = new HashMap();
		try{
			boolean retValue = false; //등록, 수정 후 리턴값
			//현재 들어 오는 곳이 등록,수정인지 구분
			String mode = BaseUtil.getParame(request.getParameter("mode"),"");
			if(mode.equals("I")){// 등록일 경우
				retValue = getInsert(request);
			}else if(mode.equals("U")){// 수정일 경우
				/**
				 * 페이지 기본값을 셋팅 해준다.
				 */
				returnValue.put("id",				BaseUtil.getParame(request.getParameter("id"),""));
				returnValue.put("searchGubn",		BaseUtil.getParame(request.getParameter("searchGubn"),""));
				returnValue.put("searchValue",		BaseUtil.getParame(request.getParameter("searchValue"),""));
				returnValue.put("txtSelectPage",	BaseUtil.getParame(request.getParameter("txtSelectPage"),""));
				returnValue.put("txtSelectBlock",	BaseUtil.getParame(request.getParameter("txtSelectBlock"),""));
				retValue = getUpdate(request);
			}
			returnValue.put("mode" , mode);
			returnValue.put("saveYn" , retValue+"");
		}catch(Exception sqlEx){
			logger.error(sqlEx);
		}
		return returnValue;
	}	
	
	/**
	 * 회원 등록일 경우
	 * @param multi
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public boolean getInsert(HttpServletRequest request){
		boolean returnValue = false;
		try{

			HashMap paramMap = new HashMap();
			String	tel	= request.getParameter("tel1")+request.getParameter("tel2")+request.getParameter("tel3");
			String	hp	= request.getParameter("hp1")+request.getParameter("hp2")+request.getParameter("hp3");
			String	jumin	= request.getParameter("jumin1")+request.getParameter("jumin2");			
			String	email = "";
			if(request.getParameter("email2").equals("")){
				email = request.getParameter("email1")+"@"+request.getParameter("email3");
			}else{
				email = request.getParameter("email1")+"@"+request.getParameter("email2");
			}
			paramMap.put("id"				, BaseUtil.getParame(request.getParameter("hiddenid"),""));
			paramMap.put("name"				, BaseUtil.getParame(request.getParameter("name"),""));
			paramMap.put("jumin"			, BaseUtil.getParame(jumin,""));
			paramMap.put("pwd"				, BaseUtil.getParame(request.getParameter("pwd"),""));
			paramMap.put("tel"				, BaseUtil.getParame(tel,""));
			paramMap.put("hp"				, BaseUtil.getParame(hp,""));
			paramMap.put("email"			, BaseUtil.getParame(email,""));
			paramMap.put("grade"			, BaseUtil.getParame(request.getParameter("grade"),""));
			paramMap.put("authorize"		, BaseUtil.getParame(request.getParameter("authorize"),""));
			
			if(sqlMapper.update("member.member_insert", paramMap) > 0){
				returnValue = true;
			}else{
				returnValue = false;
			}
		}catch(Exception sqlEx){
			logger.error(sqlEx);
		}
		return returnValue;
	}
	
	/**
	 * 회원 수정일 경우
	 * @param multi
	 * @return
	 */
	public boolean getUpdate(HttpServletRequest request){
		boolean returnValue = false;
		try{
			
			HashMap paramMap = new HashMap();
			String	tel	= request.getParameter("tel1")+request.getParameter("tel2")+request.getParameter("tel3");
			String	hp	= request.getParameter("hp1")+request.getParameter("hp2")+request.getParameter("hp3");
			String	email = "";
			if(request.getParameter("email2").equals("")){
				email = request.getParameter("email1")+"@"+request.getParameter("email3");
			}else{
				email = request.getParameter("email1")+"@"+request.getParameter("email2");
			}
			paramMap.put("pwd"				, BaseUtil.getParame(request.getParameter("pwd"),""));
			paramMap.put("tel"				, BaseUtil.getParame(tel,""));
			paramMap.put("hp"				, BaseUtil.getParame(hp,""));
			paramMap.put("email"			, BaseUtil.getParame(email,""));
			paramMap.put("grade"			, BaseUtil.getParame(request.getParameter("grade"),""));
			paramMap.put("id"				, BaseUtil.getParame(request.getParameter("id"),""));

			if(sqlMapper.update("member.member_update", paramMap)> 0){
				returnValue = true;
			}else{
				returnValue = false;
			}
		}catch(Exception sqlEx){
			logger.error(sqlEx);
		}
		return returnValue;
	}	
	
	/**
	 * 회원 삭제
	 * @param request
	 * @return
	 */
	public HashMap getDelete(HttpServletRequest request){
		HashMap returnValue = new HashMap();
		try{
			HashMap paramMap = new HashMap();
			paramMap.put("idx",	BaseUtil.getParame(request.getParameter("idx"),""));
			
			String	mode = BaseUtil.getParame(request.getParameter("mode"),"D");
			
			returnValue.put("idx", BaseUtil.getParame(request.getParameter("idx"),""));
			returnValue.put("searchGubn", BaseUtil.getParame(request.getParameter("searchGubn"),""));
			returnValue.put("searchValue", BaseUtil.getParame(request.getParameter("searchValue"),""));
			returnValue.put("txtSelectPage", BaseUtil.getParame(request.getParameter("txtSelectPage"),"0"));
			returnValue.put("txtSelectBlock", BaseUtil.getParame(request.getParameter("txtSelectBlock"),"0"));
			
			if(sqlMapper.update("member.member_delete", paramMap)> 0){
				returnValue.put("saveYn","true");
				returnValue.put("mode" , mode);
			}else{
				returnValue.put("saveYn","false");
				returnValue.put("mode" , mode);
			}
		}catch(Exception sqlEx){
			logger.error(sqlEx);
		}
		return returnValue;
	}		
	public HashMap getIdcheck(HttpServletRequest request){
		HashMap returnValue = new HashMap();
		try{
			HashMap paramMap = new HashMap();
			paramMap.put("id",	BaseUtil.getParame(request.getParameter("id"),""));
			cnt = ((Integer)sqlMapper.queryForObject("member.member_check",paramMap)).intValue();
			if(cnt <= 0){
				returnValue.put("saveYn","true");
				returnValue.put("id",request.getParameter("id"));
			}else{
				returnValue.put("saveYn","false");
			}
		}catch(Exception sqlEx){
			logger.error(sqlEx);
		}
		return returnValue;
	}		
	
	
	/**
	 * 아이디/비밀번호 찾기
	 * @param multi
	 * @return
	 */
	public HashMap getFind(HttpServletRequest request){
		HashMap returnValue = new HashMap();
		try{
			//현재 들어 오는 곳이 등록,수정인지 구분
			String mode = BaseUtil.getParame(request.getParameter("mode"),"");
			if(mode.equals("I")){
				returnValue = getFindId(request);
			}else if(mode.equals("P")){
				returnValue = getFindPwd(request);
			}
			returnValue.put("mode" , mode);
		}catch(Exception sqlEx){
			logger.error(sqlEx);
		}
		return returnValue;
	}		
	
	public HashMap getFindId(HttpServletRequest request){
		HashMap returnValue = new HashMap();
		try{
			HashMap paramMap = new HashMap();
			paramMap.put("name",	BaseUtil.getParame(request.getParameter("name"),""));
			paramMap.put("email",	BaseUtil.getParame(request.getParameter("email"),""));
			String id = (String)sqlMapper.queryForList("member.member_find_id",paramMap).toString();
			if(!id.equals("[]")){
				returnValue.put("saveYn","true");
				returnValue.put("id",id);
			}else{
				returnValue.put("saveYn","false");
			}
		}catch(Exception sqlEx){
			logger.error(sqlEx);
		}
		return returnValue;
	}	
	
	public HashMap getFindPwd(HttpServletRequest request){
		HashMap returnValue = new HashMap();
		try{
			HashMap paramMap = new HashMap();
			paramMap.put("id",	BaseUtil.getParame(request.getParameter("id"),""));
			paramMap.put("pname",	BaseUtil.getParame(request.getParameter("pname"),""));
			paramMap.put("pemail",	BaseUtil.getParame(request.getParameter("pemail"),""));
			String pwd = (String)sqlMapper.queryForList("member.member_find_pwd",paramMap).toString();
			if(!pwd.equals("[]")){
				returnValue.put("saveYn","true");
				returnValue.put("pwd",pwd);
			}else{
				returnValue.put("saveYn","false");
			}
		}catch(Exception sqlEx){
			logger.error(sqlEx);
		}
		return returnValue;
	}		
	
	public List loginOk(String login_id ,String login_pw){
		List returnValue = new ArrayList();
		HashMap map = new HashMap();
		map.put("id",login_id);
		map.put("pwd",login_pw);
		try {
			returnValue = (List)sqlMapper.queryForList("member.loginOk",map);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return returnValue;
	}	
	
	/**
	 * 주민번호 체크
	 * @param multi
	 * @return
	 */
	public boolean getJuminCheck(HashMap paramMap){
		boolean returnValue = false;
		try{
			paramMap.put("jumin",(String)paramMap.get("jumin"));			
			cnt = ((Integer)sqlMapper.queryForObject("member.jumin_Check",paramMap)).intValue();
			
			if(cnt > 0){
				returnValue = false;
			}else{
				returnValue = true;
			}
		}catch(Exception sqlEx){
			logger.error(sqlEx);
		}
		return returnValue;
	}		
	
	/**
	 * 사용자 화면 회원 삭제
	 * @param request
	 * @return
	 */
	public HashMap getUserDelete(HttpServletRequest request){
		HashMap returnValue = new HashMap();
		try{
			HashMap paramMap = new HashMap();
			
			String	mode = BaseUtil.getParame(request.getParameter("mode"),"D");
			
			paramMap.put("id", BaseUtil.getParame((String)request.getSession().getAttribute("id"),""));
			
			if(sqlMapper.update("member.user_delete", paramMap)> 0){
				returnValue.put("saveYn","true");
				returnValue.put("mode" , mode);
			}else{
				returnValue.put("saveYn","false");
				returnValue.put("mode" , mode);
			}
		}catch(Exception sqlEx){
			logger.error(sqlEx);
		}
		return returnValue;
	}			
}
